package com.guoxianwei.scm.web.actions;

import java.io.StringReader;
import java.util.List;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SessionScope;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import com.guoxianwei.scm.domain.Account;
import com.guoxianwei.scm.domain.Menu;
import com.guoxianwei.scm.service.MenuService;
import com.guoxianwei.util.MenuTreeHelper;

@SessionScope
public class MenuActionBean extends AbstractActionBean {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5042665499867714155L;

	private static final String MAIN = "/WEB-INF/jsp/catalog/index.jsp";

	@SpringBean
	private transient MenuService menuService;
	@DefaultHandler
	public Resolution searchMenus() {
		Account account = super.getAccount();
		List<Menu> menuList = menuService.getMenuListByUser(String
				.valueOf(account.getUserid()));

		StreamingResolution resolution = new StreamingResolution(
				"text",
				new StringReader(MenuTreeHelper.getMenuTreeJsonString(menuList)));
		
		return resolution;
	}
	
	public Resolution viewMain(){
	    return new ForwardResolution(MAIN);
	}
}
